#!/bin/bash
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 13 November 2012
#
#   Copyright 2012-2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## p_agent_rsync_help
##
## Echoes help information for the rsync agent. Returns 0.
##
function p_agent_rsync_help() {
	cat << EOF
Uses rsync to synchronize a local clone of a directory containing arbitrary
files. The rsync URL is set using the agent/rsync/url context item. If unset,
this agent does nothing. Otherwise, the "sync" directory on the remote server
is copied into the Pulley state directory on the local machine.

In check-only mode, this agent does nothing.
EOF
	return 0
}


## p_agent_rsync_summary
##
## Echoes summary information for the rsync agent. Returns 0.
##
function p_agent_rsync_summary() {
	echo "Synchronizes a remote directory using rsync"
	return 0
}


## p_agent_rsync_check
##
## Currently unimplemented. Returns 0.
##
function p_agent_rsync_check() {
	return 0
}


## p_agent_rsync_run
##
## Runs the rsync agent. Returns 0 on success, non-zero if an error occurs.
##
function p_agent_rsync_run() {
	local status=0
	local url=$(get_context agent/rsync/url)
	mkdir -p "${P_STATEDIR}/rsync"
	status=$?
	if [[ ${status} -eq 0 && -n "${url}" ]]; then
		rsync -avmzSP --numeric-ids --delete --delete-delay --delay-updates \
			"${url}" "${P_STATEDIR}/rsync"
		#
		status=$?
	fi
	return ${status}
}
